var request = require('request');
var moment = require('moment');
moment.locale('zh-cn');

class TUtils {

    static httpCall(aUrl, aMethod, aPs, aBody) {
        return new Promise((success, faild) => {
            try {
                var callUrl = aUrl;
                if (aPs && aPs != null && Object.keys(aPs) && Object.keys(aPs).length > 0) {
                    callUrl += callUrl.indexOf("?") > 0 ? "&" : "?";
                    callUrl += "randtm=" + Math.random();
                    Object.keys(aPs).forEach(p => {
                        callUrl += "&" + p + "=" + aPs[p];
                    })
                }

                var opts = {
                    url: callUrl,
                    method: aMethod
                }

                if (aBody && aBody != null) {
                    opts.body = aBody;
                }

                request(opts, (err, resp, result) => {
                    success({
                        err: err,
                        resp: resp,
                        result: result
                    })
                })
            } catch (Exce) {
                console.log(Exce);
                success(null);
            }
        })
    }
    static writeDB(aDbUrl, dbName, tableName, tags, fields, timestampe) {
        return new Promise((success, faild) => {
            try {
                var ps = {
                    db: dbName
                };
                var tagDatas = [];
                Object.keys(tags).forEach(t => {
                    tagDatas.push(t + "=" + tags[t]);
                })
                var fieldDatas = [];
                Object.keys(fields).forEach(f => {
                    var v = fields[f];
                    if (v && v != null) {
                        fieldDatas.push(f + "=" + fields[f]);
                    }
                })
                var body = tableName + "," + tagDatas.join(",") + ' ' + fieldDatas.join(",") + " " + timestampe;
                var url = aDbUrl + "/write";
                TUtils.httpCall(url, "POST", ps, body).then(res => {
                    success(res);
                })
            } catch (Exce) {
                console.log(Exce);
                success(null);
            }
        })
    }
    static writeDBBatch(aDbUrl, dbName, tableName, dataItems) {
        return new Promise((success, faild) => {
            try {
                var items = JSON.parse(JSON.stringify(dataItems));
                var writeItem = function() {
                    if (items.length > 0) {
                        var item = items.shift();
                        TUtils.writeDB(aDbUrl, dbName, tableName, item.tags, item.fields, item.timestampe).then(_ => {
                            writeItem();
                        });
                    } else {
                        success();
                    }
                }
                writeItem();
            } catch (Exce) {
                console.log(Exce);
                success(null);
            }
        })
    }
    static getTimestampe(strTime) {
        var res = (new Date()).getTime() * 1000 * 1000;
        try {
            var s = strTime.indexOf("T") > 0 ? strTime : strTime.replace(/-/g, "/");
            res = (new Date(Date.parse(s))) * 1000 * 1000
        } catch (er) {
            console.log(er);
        }
        return res;
    }
}


module.exports = {
    TUtils: TUtils
}